package weaver.jobs.suntai;

import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;

import java.util.Calendar;
import java.util.Date;

/**
 * @Title: ecology
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2020-10-30
 * @Description: 根据开始日期和结束日期,写个计划任务,当前日期不属于这个时间段的,
 *              是否生效字段改为失效,是属于这个时间段的,是否生效字段改为生效
 * @ModifyLog:
 **/

public class WeaEffectiveStateCronJob extends BaseCronJob {
    @Override
    public void execute() {
        BaseBean baseBean = new BaseBean();
        baseBean.writeLog( "-----------WeaEffectiveStateCronJob------------" );
        String curdate = DateUtil.getCurrentDate();
        baseBean.writeLog( ">>>>>>>curdate="+curdate );
        RecordSet rs = new RecordSet();
        String sql = "select id,sqyxqx,jsrq from uf_sqwtsjmb where sfsx is null or sfsx=1 ";
        rs.executeQuery(sql);
        while (rs.next()){
            String id = Util.null2String(rs.getString( "id" ));
            String sqyxqx = Util.null2String(rs.getString( "sqyxqx" ));
            String jsrq = Util.null2String(rs.getString( "jsrq" ));
            if(isEffectiveDate(curdate,sqyxqx,jsrq)){
                rs.executeUpdate("update uf_sqwtsjmb set sfsx=0 where id=",id);
            }else {
                rs.executeUpdate("update uf_sqwtsjmb set sfsx=1 where id=",id);
            }

        }

    }

    public static boolean isEffectiveDate(String nowdate, String begdate, String enddate) {
        Date startTime =  DateUtil.parseToDateTime( begdate );
        Date endTime =  DateUtil.parseToDateTime( enddate);
        Date nowTime =  DateUtil.parseToDateTime(nowdate);
        if (nowTime.getTime() == startTime.getTime() || nowTime.getTime() == endTime.getTime()) {
            return true;
        }
        Calendar date = Calendar.getInstance();
        date.setTime( nowTime );
        Calendar begin = Calendar.getInstance();
        begin.setTime( startTime );
        Calendar end = Calendar.getInstance();
        end.setTime( endTime );
        if (date.after( begin ) && date.before( end )) {
            return true;
        } else {
            return false;
        }
    }


}
